import time
from django.shortcuts import render, HttpResponse, redirect
from django.views import View
from django.http import JsonResponse
from web.forms.project import ProjectModelForm
from web import models
from utils.tencent.cos import CosBucket


class ProjectList(View):

    def get(self, request):
        form = ProjectModelForm(request)
        '''
                    1.从数据库获取
                        我创建的：星标，未星标
                        我参与的：星标，未星标
                    2.提取已星标
                        列表 = 循环[我创建的所有] + [我参与的所有]
                    '''
        project_dict = {'my': [], 'star': [], 'join': []}
        my_project_list = models.Project.objects.filter(creator=request.tracer.user)
        for item in my_project_list:
            if item.star:
                project_dict['star'].append({'value': item, 'type': 'my'})
            project_dict['my'].append(item)

        join_project_list = models.ProjectUser.objects.filter(user=request.tracer.user)
        for item in join_project_list:
            if item.star:
                project_dict['star'].append({'value': item.project, 'type': 'join'})
            project_dict['join'].append(item)

        return render(request, 'project_list.html', {'form': form, 'project_list': project_dict})

    def post(self, request):
        form = ProjectModelForm(request, data=request.POST)
        if form.is_valid():
            # 创建桶
            bucket_name = "{}-{}-1313889948".format(request.tracer.user.phone, str(int(time.time() * 1000)))
            region = 'ap-nanjing'

            # 实例化桶对象
            cos = CosBucket(region)
            # 创建桶
            cos.create_bucket(bucket_name)
            # 创建项目 项目名、颜色、描述、用户、 cos桶
            form.instance.bucket = bucket_name
            form.instance.region = region
            form.instance.creator = request.tracer.user
            form.save()

            return JsonResponse({'status': True, 'code': 'ok', 'error': form.errors})
        return JsonResponse({'status': False, 'error': form.errors})


def project_star(request, project_type, project_id):
    """ 星标项目 """
    if project_type == "my":
        is_star = models.Project.objects.filter(id=project_id, creator=request.tracer.user, star=True)
        if is_star:
            models.Project.objects.filter(id=project_id, creator=request.tracer.user).update(star=False)
        else:
            models.Project.objects.filter(id=project_id, creator=request.tracer.user).update(star=True)
        return redirect('project_list')

    if project_type == 'join':
        is_star = models.ProjectUser.objects.filter(project_id=project_id, user=request.tracer.user, star=True)
        if is_star:
            models.ProjectUser.objects.filter(project_id=project_id, user=request.tracer.user).update(star=False)
        else:
            models.ProjectUser.objects.filter(project_id=project_id, user=request.tracer.user).update(star=True)
        return redirect('project_list')

    return HttpResponse('请求错误')


def project_unstar(request, project_type, project_id):
    if project_type == 'my':
        models.Project.objects.filter(id=project_id, creator=request.tracer.user).update(star=False)
        return redirect('project_list')

    if project_type == 'join':
        models.ProjectUser.objects.filter(project_id=project_id, user=request.tracer.user).update(star=False)
        return redirect('project_list')

    return HttpResponse('请求错误')
